/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tyrantgit.sample.slice;

import com.tyrantgit.explosionfield.ExplosionField;
import com.tyrantgit.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * DEMO样例
 *
 * @since 2021-04-08
 */
public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
    private final int num2 = 2;
    private final int num4 = 4;
    private final int num10 = 10;
    private final int num130 = 130;
    private ExplosionField mExplosionField;
    private List<Component> components;
    private int[] ids = {ResourceTable.Id_image1, ResourceTable.Id_image2, ResourceTable.Id_image3,
        ResourceTable.Id_image4, ResourceTable.Id_image5, ResourceTable.Id_image6, ResourceTable.Id_text };
    private Image undoBtn;

    /**
     * 给组件设置监听
     *
     * @param component
     */
    public void setListener(Component component) {
        component.setClickedListener(comp -> {
            mExplosionField.explode(comp);
            comp.setClickedListener(null);
        });
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);

        Component rootLayout = this.findComponentById(ResourceTable.Id_root);
        undoBtn = (Image) this.findComponentById(ResourceTable.Id_undo_btn);
        undoBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DirectionalLayout menuComponent = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_custom_setting_popup, null, false);
                Text textSetting = (Text) menuComponent.findComponentById(ResourceTable.Id_text);
                DirectionalLayout dl = (DirectionalLayout) menuComponent.findComponentById(ResourceTable.Id_dl);
                textSetting.setWidth(getWindow().getLayoutConfig().get().width / num2);
                ShapeElement element = new ShapeElement();
                element.setCornerRadius(num10);
                dl.setBackground(element);
                textSetting.setBackground(element);
                Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(getContext());
                DisplayAttributes displayAttributes = display.get().getAttributes();
                PopupDialog menuDialog = new PopupDialog(getContext(), null);
                menuDialog.setCornerRadius(num10);
                menuDialog.setCustomComponent(menuComponent);
                menuDialog.showOnCertainPosition(1,
                        displayAttributes.width / num4 - AttrHelper.vp2px(num10, getContext()),
                        -displayAttributes.width + num130);
                menuDialog.setAutoClosable(true);
                menuDialog.setAlignment(LayoutAlignment.RIGHT | LayoutAlignment.TOP);
                menuDialog.show();
                textSetting.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        reset();
                        menuDialog.destroy();
                    }
                });
            }
        });
        mExplosionField = ExplosionField.attach2Window(this.getAbility(), rootLayout);

        components = new ArrayList<Component>();
        for (int i = 0; i < ids.length; i++) {
            Component component = this.findComponentById(ids[i]);
            components.add(component);
            setListener(component);
        }
    }

    /**
     * 重置
     */
    public void reset() {
        for (Component component : components) {
            component.setScaleX(1);
            component.setScaleY(1);
            component.setAlpha(1);
            setListener(component);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        return false;
    }
}
